/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;


import Conn.mysqlconnect;
import ModelKelas.Admin;
import com.sun.xml.internal.bind.v2.model.core.Adapter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author valentinus
 */
public class AdminControl {

    ResultSet rs = null;
    PreparedStatement pst = null;
    private Connection conn;

    public AdminControl(Connection koneksi) {
        this.conn = koneksi;
    }

    public static AdminControl getkoneksi() throws SQLException {
        AdminControl kon = new AdminControl(mysqlconnect.ConnectDb());
        return kon;
    }

    public void tambahUser(Admin admin) throws SQLException {

        String nik = admin.getNIK();
        String nama = admin.getNamaKaryawan();
        String password = admin.getPassword();
        String jabatan = admin.getJabatan();
        String sql = "INSERT INTO User (NIK ,Nama, Password, Jabatan) VALUES "
                + "('" + nik + "','" + nama + "','" + password
                + "','" + jabatan + "')";
        Statement stat = (Statement) conn.createStatement();
        stat.executeUpdate(sql);
    }

    public void deleteUser(Admin adm) throws SQLException {
        PreparedStatement stmt = null;
        try {
            conn.setAutoCommit(false);
            String query = "delete from user where nik=?";
            stmt = conn.prepareStatement(query);
            stmt.setString(1, adm.getNIK());
            stmt.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (stmt != null) {
                    stmt.close();
                }
            } catch (Exception e) {
                try {
                    throw e;
                } catch (Exception ex) {
                    Logger.getLogger(AdminControl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

   public void cariUser(Admin admin) throws SQLException {
        Statement statement = null;
        ResultSet result = null;

        try {
            statement = conn.createStatement();
            String sql = "SELECT Nama, PASSWORD, Jabatan FROM USER WHERE nik='"+admin.getNIK()+"'";
            result = statement.executeQuery(sql);
            while (result.next()) {
                admin.setNamaKaryawan(result.getString("Nama"));
                admin.setPassword(result.getString("Password"));
                admin.setJabatan(result.getString("Jabatan"));
            }
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(AdminControl.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}
